<?php
/**
 * Author Yulin
 * Email  dwzhanglong@126.com
 * Date   2016-6-21
 * Func   菜单管理
 */
defined('IN_YuLin') || exit('NO PERMIT!');

$table = Table('link');
if(IS_POST){
    if($a == 'save'){
        $ids = $_POST['ids'];
        foreach ($ids as $k => $v) {
            $ids[$k] = intval(NoBadStr($v));
        }
        $findSql = "select id from ".Table('lottery')." where id in (".implode(',', $ids).") and recommend = 1";
        $rows = $db->affected_rows($findSql);
        $rows && AjaxReturn(['error'=>'0','msg'=>'该彩种已经添加了']);
        $db->exec("UPDATE ".Table('lottery')." SET recommend = 1,hottime = ".time()." WHERE id in (".implode(',', $ids).")");
        AjaxReturn(['error'=>'0','msg'=>'保存成功']);
        // $ids = $_POST['ids'];
        // $insertSql = "insert into ".Table('lottery_curr')." (`hid`,`inputtime`) values ";
        // foreach ($ids as $k => $v) {
        //     $ids[$k] = intval(NoBadStr($v));
        //     $str = ($k == (count($ids) - 1)) ? '':',';
        //     $insertSql .= "(".intval(NoBadStr($v)).", ".time().")".$str;
        // }
        // $totalSql = "select id from ".Table('lottery_curr');
        // $total = $db->affected_rows($totalSql);
        // if(intval($total) + count($ids) > 10)AjaxReturn(['error'=>'0','msg'=>'最多只能添加10种彩种']);
        // $idsStr = implode(',', $ids);
        // $findSql = "select id from ".Table('lottery_curr')." where hid in (".$idsStr.")";
        // $rows = $db->affected_rows($findSql);
        // $rows && AjaxReturn(['error'=>'0','msg'=>'该彩种已经添加了']);
        // $res = $db->exec($insertSql);
        // AjaxReturn(['error'=>'0','msg'=>'保存成功']);
    }elseif($a == 'delete'){
        $ids = $_POST['ids'];
        foreach ($ids as $k => $v) {
            $ids[$k] = intval(NoBadStr($v));
        }
        $findSql = "select id from ".Table('lottery')." where id in (".implode(',',$ids).") and recommend = 1";
        $rows = $db->affected_rows($findSql);
        $rows != count($ids) && AjaxReturn(['error'=>'0','msg'=>'参数错误']);
        $db->exec("UPDATE ".Table('lottery')." SET recommend = 0 WHERE id in (".implode(',',$ids).")");
        AjaxReturn(['error'=>'0','msg'=>'成功删除'.$rows."条热门彩种"]);

        // $ids = $_POST['ids'];
        // foreach ($ids as $k => $v) {
        //     $ids[$k] = intval(NoBadStr($v));
        // }
        // $idsStr = implode(",", $ids);
        // $findSql = "select id from ".Table('lottery_curr')." where hid in (".$idsStr.")";
        // $rows = $db->affected_rows($findSql);
        // $rows != count($ids) && AjaxReturn(['error'=>'0','msg'=>'参数错误']);
        // $db->exec("delete from ".Table('lottery_curr')." where hid in (".$idsStr.")");
        // AjaxReturn(['error'=>'0','msg'=>'成功删除'.$rows."条热门彩种"]);
    }else{
        $id = intval($_REQUEST['id']);
        $data = $id ? $db->getrow('SELECT * FROM '.$table.' WHERE id = '.$id) : [];

        //  表单数据
        $post = $_POST['data'];

        $ins = [];
        $ins['title']    =   NoBadStr($post['title']);
        $ins['summary']       =   $post['summary'];
        $ins['url']      =   NoBadStr($post['url']);
        $ins['listsort']      =   NoBadStr($post['listsort']);

        if($data['id']){
            //修改
            $db->exec('UPDATE '.$table.' SET '.CreateUpdateSql($ins).' WHERE id = '.$data['id']);
        }else{
            $ins['dateline']    =   time();
            $db->exec('INSERT INTO '.$table.CreateInsertSql($ins));
        }
        AjaxReturn(['error'=>'0','msg'=>'操作成功']);        
    }
}

if($a == 'add'){
    $id=NoBadStr($_REQUEST['id']);
    $data=$db->getrow("select * from ".$table." where id='{$id}'");
    $tpl->display($m.'/linkadd');
}elseif($a == 'del'){
    $id = NoBadStr($_REQUEST['id']);
    if(!$id){
        AjaxReturn(['error'=>'1','msg'=>'参数丢失']);
    }
    $db->exec('DELETE FROM '.$table.' WHERE id = '.$id);
    AjaxReturn(['error'=>'0','msg'=>'删除成功']);
}else{
    // 热门彩种列表
    $hotLotterySql = "select `id`,`title` from ".Table('lottery');
    $hotLotteryRes = $db->getall($hotLotterySql);

    // 当前选中热门彩种列表
    $hotLotteryCurrSql = "SELECT id,title,hottime AS inputtime FROM ".Table('lottery')." WHERE recommend = 1 ORDER BY listorder ASC";
    $hotLotteryCurrRes = $db->getall($hotLotteryCurrSql);
    // $hotLotteryCurrSql = "select b.`id`,b.`title`,a.`inputtime` from ".Table('lottery_curr')." as a left join ".Table('lottery')." as b on a.hid = b.id";
    // $hotLotteryCurrRes = $db->getall($hotLotteryCurrSql);

    // $data  = array();
    // $where = ' FROM '.$table.' t WHERE 1 ';

    // $tmp = $db->getrow('SELECT COUNT(1) AS p '.$where);
    // if($tmp['p']){
    //     $page = max(1,intval($_GET['page']));
    //     $size = 20;
    //     $limit    = ' LIMIT '.($page - 1) * $size.','.$size;

    //     $orderby = ' ORDER BY t.listsort ASC ';
    //     $url = '?m='.$m.'&c='.$c.'&a='.$a;
    //     $data = $db->getall('SELECT t.* '.$where.$orderby.$limit);
    //     $multi = Multi($url,$page,$tmp['p'],$size);
    // }
    $tpl->display($m.'/lotteryhot');
}
?>